#include "netconnect.h"
#include "protocol.h"

#include "lvgl.h"
#include "lv_port_disp.h"
#include "lv_port_indev.h"

int socket_desc;


void ncmemcp(unsigned char * pof,unsigned char * pif,unsigned int dlen)
{
    unsigned int i;
    for ( i = 0; i < dlen; i++)
    {
        pof[i] = pif[i];
    }
}

touch_t  touchnet=
{
    .tf = 0,
};

/* ------------------------------------------------------------------------- */
void smemccpy(void *pof,void *pif,unsigned int count)
{
    unsigned char * poft;
    unsigned char * pift;
    int i;

    poft = (unsigned char *)pof;
    pift = (unsigned char *)pif;
    for ( i = 0; i < count; i++)
    {
        poft[i] = pift[i];
    }
    
}
/* ------------------------------------------------------------------------- */
char  server_reply[2000];
char  protocalbuf[1024];
void *task_netconnect(void * arg)
{
    int relen;
    int i;
    int ii;

    protocol_t protocol;

    protocol.pbuf = protocalbuf;
    protocol.pbufs = 0;
    protocol.bufsize = 1024;
    protocol.datacnt = 0;
    protocol.step = 0;
    protocol.targcnt = 0;

    ii = 0;
    while (1)
    {

        relen = recv(socket_desc, server_reply, 1000, 0);
        if (relen < 0) 
        {
            perror("recv error");
            //return 3;
        }
        else
        {
            for ( i = 0; i < relen; i++)
            {
                if(ProtocolRun(&protocol,server_reply[i]))
                {
                    
                    if(protocol.pbuf[5] == 0x01)
                    {
                        touchnet.tf = 0;
                        touchnet.type = 1;
                        ncmemcp((unsigned char *)&touchnet.tx,&protocol.pbuf[6],4);
                        ncmemcp((unsigned char *)&touchnet.ty,&protocol.pbuf[10],4);
                        touchnet.tf = 1;
                        printf("recive data[%d](%d,%d)(%d)!!!\r\n",relen,touchnet.tx,touchnet.ty,ii++);
                    }

                    if(protocol.pbuf[5] == 0x02)
                    {
                        touchnet.tf = 0;
                        touchnet.type = 0;
                        ncmemcp((unsigned char *)&touchnet.tx,&protocol.pbuf[6],4);
                        ncmemcp((unsigned char *)&touchnet.ty,&protocol.pbuf[10],4);
                        touchnet.tf = 1;
                        printf("recive data[%d](%d,%d)(%d)!!!\r\n",relen,touchnet.tx,touchnet.ty,ii++);
                    }

                    if(protocol.pbuf[5] == 0x03)
                    {
                        lv_tick_inc(20);
                        //printf("tick %d\r\n",ii++);
                    }

                }
            }
        }
    }
}

void sendoneimg(unsigned int sx,unsigned int sy,unsigned int ex,unsigned int ey,unsigned char *color_p)
{
    //NETPRINT_T      img1;
    unsigned int     dcnt;
    unsigned int     dcntn;
    int i,ii;
    char *           message;

    message = malloc((ex-sx+1)*(ey-sy+1)*2+10+16+100);
    message[0] = 0xaa;
    message[1] = 0x55;
    dcnt = (ex-sx+1)*(ey-sy+1)*2+16;
    dcntn = (~dcnt)&0xffffffff;
    smemccpy(&message[2],&dcnt,4);
    smemccpy(&message[6],&dcntn,4);
    smemccpy(&message[10],&sx,4);
    smemccpy(&message[14],&sy,4);
    smemccpy(&message[18],&ex,4);
    smemccpy(&message[22],&ey,4);
        
    smemccpy(&message[26],color_p,(ex-sx+1)*(ey-sy+1)*2);
        

    if (send(socket_desc, message, (ex-sx+1)*(ey-sy+1)*2+10+16, 0) < 0) {
        // perror("send data error");
        // return 2;
    }

    //printf("send message success\n");

    if(message)
    {
        free(message);
    }
}